<?php
class userApp extends core
{
	public function logoutAction()
	{
		user::logout();
		$this->refresh('退出成功',".");
	}

	public function mainAction($arg)
	{
		$template		= $this->template();
		$users			= base::init('user')->fetchAll(null,'id');
		$websites		= base::init('website')->fetchAll();
		$groups			= sort_combine(base::init('group')->fetchAll(),'wid',false);

		$template->assign('users',$users);
		$template->assign('groups',$groups);
		$template->assign('websites',$websites);
		$template->display('user_main.php');
	}
	
	public function repassAction($arg)
	{
		if (isset($arg['ajax']))
		{
			$where	= array('id'=>$arg['id']);
			$edit	= array('password'=>$arg['password']);
			if (base::init('user')->edit($where, $edit))
				print_r('1');
		}
	}
	
	public function deleteAction($arg)
	{
		if (isset($arg['ajax']))
		{
			$uid	= array('id'=>explode(',',$arg['id']));
			if (base::init('user')->delete($uid))
				print_r('1');
		}
	}
	
	public function addAction($arg)
	{
		if (base::init('user')->insert($arg))
			$this->refresh('添加成功',"index.php?m=user&a=main");
	}

    public function disperAction($arg)
    {
        if ( isPost() )
        {
            #print_r($arg);exit;
            $where  = array('id'=>$arg['id']);
            $edit   = array('gid'=>join(',',$arg['gid']));
            base::init('user')->edit($where, $edit);
            $this->refresh('修改成功','index.php?m=user&a=main');
        }
        else 
        {
            $template   = $this->template();
            $user       = base::init('user')->getRow(array('id'=>$arg['id']));
            $websites   = base::init('website')->fetchAll();
            $groups     = base::init('group')->fetchAll();
            $groups     = sort_combine($groups, 'wid', false);

            $template->assign('user', $user);
            $template->assign('websites', $websites);
            $template->assign('groups', $groups);
            $template->display('user_disper.php');
        }
    }

	public function mkpasswdAction($arg)
    {
    	exit(randStr(11));
    }
}
